﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;


using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_CourseManager_ResourceList : PowerCreator.LMS.Web.Base.BasePage
{
    protected Course opCourse = new Course();
    PowerCreator.LMS.Web.Course.Resource rsA = new PowerCreator.LMS.Web.Course.Resource();
    ManagerPaper opPaper = new ManagerPaper();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";//0简单搜索1高级搜索
            ViewState["OrderList"] = " FKCoursersResourceID desc";

            string courseid = "0";

            if (Request.QueryString["CourseID"] != null)
            {
                courseid = Request.QueryString["CourseID"].ToString();
            }
            ViewState["CourseID"] = courseid;

            InitPage();
            BinderCoursersType();
          
          
            
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
            BinderResourceClass();
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_ResourceList_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_ResourceName.Text = GetLanageValueByKey("lbl_ResourceList_ResourceName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CoursersClass.Text = GetLanageValueByKey("lbl_ResourceList_CoursersClass", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CoursersRestp.Text = GetLanageValueByKey("lbl_ResourceList_CoursersRestp", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Search.Value = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Upload.Value = GetLanageValueByKey("btn_ResourceList_Upload", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AuditingStateYes.Text = GetLanageValueByKey("TeacherManager_gv_c1", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AuditingStateNo.Text = GetLanageValueByKey("depratment_attributes_classstate0", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_del.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o2", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddCourseResource.Value = GetLanageValueByKey("CourseResource_gv_ln2", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddPaper.Value = GetLanageValueByKey("CourseResource_gv_ln3", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddNew.Value = GetLanageValueByKey("btn_ResourceList_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Back.Text = GetLanageValueByKey("btn_ResourceList_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");

        VOTOCourse voCourse = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        lbl_CourseName.Text = string.Format("( {0} 资源)", voCourse.CourseResourceNum.ToString());
        lnkbtn_CourseName.Text = voCourse.CourseName;
        lnkbtn_CourseName.ToolTip = voCourse.Description;

        base.InitPage();
    }
    private void BinderResourceClass()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Resource_Type", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "-1";
        ddl_CoursersClass.Items.Insert(0, li);
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_CoursersClass.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li2.Value = "2";
        ddl_CoursersClass.Items.Add(li2);
    }
    private void BinderCoursersType()
    {
        DataTable dt = new DataTable();
        string search = "";
        dt = opCourse.QueryResourceType(search);
        ArrayList list = opCourse.GetResourceTypeTree(dt);
        ddl_CoursersRestp.DataSource = list;
        ddl_CoursersRestp.DataTextField = "Text";
        ddl_CoursersRestp.DataValueField = "Value";
        ddl_CoursersRestp.DataBind();

        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ddl_CoursersRestp.Items.Insert(0, li);
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(string.Format("FKCourseresCourseID = {0} and ", ViewState["CourseID"]));

        if (ViewState["SearchType"].ToString() == "1")
        {
            if (ddl_CoursersClass.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format(" CoursersClass = {0} and ", ddl_CoursersClass.SelectedValue.ToString()));
            }

            if (ddl_CoursersRestp.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format(" FKCoursersRestpID = {0} and ", ddl_CoursersRestp.SelectedValue.ToString()));
            }

            if (!string.IsNullOrEmpty(txt_ResourceName.Value))
            {
                sbSearch.Append(string.Format(" FKCoursersResourceID in (select ResourceID from TResource where ResourceName like '%{0}%') and ", txt_ResourceName.Value));
            }
        }
        else if (ViewState["SearchType"].ToString() == "0" && !string.IsNullOrEmpty(txt_SearchKey.Text))
        {
            sbSearch.Append(string.Format(" FKCoursersResourceID in (select ResourceID from TResource where ResourceName like '%{0}%' or ResourceAuthor like '%{0}%' and ResourceDescription like '%{0}%')", txt_SearchKey.Text));
        }

        if (sbSearch.ToString().EndsWith(" and "))
        {
            sbSearch.Remove(sbSearch.Length - 4, 4);
        }

        return sbSearch.ToString();

    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opCourse.QueryCourseResourceCount(search);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        string orderList = ViewState["OrderList"].ToString();
        //Response.Write(search);
        DataTable dt = opCourse.QueryCourseResource(search,orderList, LMSPageSize, pageIndex);
        List<CourseResourceInfo> list = FillList(dt);
        rptResource.DataSource = list;
        rptResource.DataBind();
    }


    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }

    //DataTable为多表联链接查询后的数据
    private List<CourseResourceInfo> FillList(DataTable dt)
    {
        List<CourseResourceInfo> list = new List<CourseResourceInfo>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CourseResourceInfo item = new CourseResourceInfo();
            item.CourseID = int.Parse(dt.Rows[i]["FKCourseresCourseID"].ToString());
            item.CourseresID = int.Parse(dt.Rows[i]["CourseresID"].ToString());
            item.CoursersRestpID = int.Parse(dt.Rows[i]["FKCoursersRestpID"].ToString());
            VOTResourceType voResourceType = opCourse.LoadResourceType(item.CoursersRestpID);
            item.CoursersRestpName = voResourceType.RestpName;
            item.CoursersClassID = int.Parse(dt.Rows[i]["CoursersClass"].ToString());
            item.CoursersClassName = opCourse.GetCoursersClassByData(item.CoursersClassID, GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page));

            if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Courseware))  //课件资源
            {
                item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersResourceID"].ToString());
                VOTResource voResource = opCourse.LoadResource(item.CoursersResourceID);
                
                item.ResourceName = voResource.ResourceName;
                item.VOTresourceState = voResource.ResourseState;
            }
            else if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Paper)) //课程资源
            {
                item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersPaperID"].ToString());
                VOPaper voPaper = opPaper.LoadPaper(item.CoursersResourceID);
                item.ResourceName = voPaper.PaperName;
                item.VOTresourceState = 1;
            }

            list.Add(item);
        }
        return list;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_ServerClick(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";//0简单搜索1高级搜索
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }


    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        string url = "UploadResource.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_AddNew_Click(object sender, EventArgs e)
    {
        string url = "AddResourceInfo.aspx?CoursersClass=1&AddResourceType=addnew&CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_AddCourseResource_Click(object sender, EventArgs e)
    {
        string url = "ResourceSelect.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_AddPaper_Click(object sender, EventArgs e)
    {
        string url = "PaperSelect.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "CourseResource.aspx";
        Script.RedirectURL(url);
    }
    protected void rptResource_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_resourcetype_paper");

        if (e.CommandName.ToLower() == "ledit")
        {
            VOTCourseResource vo = opCourse.LoadCourseResource(int.Parse(e.CommandArgument.ToString()));
            if (vo.CoursersClass == Convert.ToInt16(CoursersClass.Paper))
            {
               // lbl_msg.Text = "试卷资源不允许编辑!";
                lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m1", PowerCreator.LMS.Language.LanagetItemType.Page);
                return;
            }
            else if (vo.CoursersClass == Convert.ToInt16(CoursersClass.Courseware))
            {
                string url = "EditResourceInfo.aspx?CourseresID=" + e.CommandArgument.ToString();
                Script.RedirectURL(url);
            }
        }
        else if (e.CommandName.ToLower() == "lshow")
        {
            //打开看看
            VOTCourseResource vo = opCourse.LoadCourseResource(int.Parse(e.CommandArgument.ToString()));
            if (vo.CoursersClass == 2)
            {
                if (vo.FKCoursersPaperID > 0)
                {
                    string url = WebUtils.WebRoot + "/sysadmin/QuestionManager/ShowPaperInfo.aspx?p=" + vo.FKCoursersPaperID.ToString();
                    Script.NewOpenURL(url);
                }
            }
            else if(vo.CoursersClass == 1)
            {
                if (vo != null)
                {
                    
                    VOTResource voResource = opCourse.LoadResource(vo.FKCoursersResourceID);
                    if (voResource != null)
                    {
                        string url = voResource.ResourceUrl + "/" + voResource.ResourceEntry;
                        url = url.Replace("\\", "/");
                        url = url.Replace("//","/");
                        
                        url = WebUtils.WebRoot + url;
                        if (rsA.ExistsResourcePath(url))
                        {
                            Script.NewOpenURL(url, "testfileurl");
                        }
                        else
                        {
                            lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page);

                        }
                        
                    }
                    else
                    {
                       // lbl_msg.Text = "找不到该资源信息!是否己删除!";
                        lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page);
                    }
                }
                else
                {
                    //lbl_msg.Text = "找不到该课程资源信息!请检查该资源是否被删除!";
                    lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m3", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
            }
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //if (IsLicensed("CoursewareDel"))
            //{
                if (opCourse.DelCourseResource(int.Parse(e.CommandArgument.ToString())))
                {
                    string search = GetSearch();
                    BinderPager(search);
                    BinderRPT(search, AspNetPager1.CurrentPageIndex);
                }
                InitPage();
            //}
            //else
            //{
            //    //lbl_msg.Text = "您没有删除课件资源的权限!";
            //    lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m4", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }
        else if (e.CommandName.ToLower() == "lpl")
        {
            //if (IsLicensed("CoursewareCommentManager"))
            //{
                //管理点评
                VOTCourseResource vo = opCourse.LoadCourseResource(int.Parse(e.CommandArgument.ToString()));
                if (vo.CoursersClass == Convert.ToInt16(CoursersClass.Paper))
                {
                    //lbl_msg.Text = "该资源信息为试卷类型，没有点评功能!";
                    lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m5", PowerCreator.LMS.Language.LanagetItemType.Page);
                }else
                {
                    string url = "ResourceComment.aspx?ResourceID=" + vo.FKCoursersResourceID.ToString();
                    Script.RedirectURL(url);
                }
            //}
            //else
            //{
            //    //lbl_msg.Text = "您没有管理课件点评功能的权限!";
            //    lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m6", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }
        else if (e.CommandName.ToLower() == "lstate")
        {
            VOTCourseResource vo = opCourse.LoadCourseResource(int.Parse(e.CommandArgument.ToString()));
            if (vo.CoursersClass == Convert.ToInt16(CoursersClass.Paper))
            {
                lbl_msg.Text = "该资源信息为试卷类型，不支持试卷审核!";
                lbl_msg.Text = GetLanageValueByKey("ResourceList_msg_m7", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            else
            {
                VOTResource voResource = opCourse.LoadResource(vo.FKCoursersResourceID);
                //voResource.ResourseState += 1;
                if (voResource.ResourseState == 1)
                {
                    voResource.ResourseState = 0;
                }
                else
                {
                    voResource.ResourseState = 1;
                }
                if (opCourse.UpdateResource(voResource))
                {
                    string search = GetSearch();
                    BinderPager(search);
                    BinderRPT(search, AspNetPager1.CurrentPageIndex);
                }
            }
        }
        else if (e.CommandName.ToLower() == "add")
        {
            string url = "UploadDownLoadResource.aspx?ResourceID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
    }
    protected void btn_AddPaper_ServerClick(object sender, EventArgs e)
    {
        //引用试卷
        string url = "PaperSelect.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_AddCourseResource_ServerClick(object sender, EventArgs e)
    {
        //引用现有课件资源
        string url = "ResourceSelect.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_AddNew_ServerClick(object sender, EventArgs e)
    {
        //添加新资源记录
        string url = "AddResourceInfo.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_Upload_ServerClick(object sender, EventArgs e)
    {
        //上传资源
        string url = "UploadResource.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选/取消
        for (int i = 0; i < rptResource.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptResource.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptResource.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptResource.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_AuditingStateYes_Click(object sender, EventArgs e)
    {
        //审核通过
        for (int i = 0; i < rptResource.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptResource.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptResource.Items[i].FindControl("lblid");
                Label lbl_resourcetype = (Label)rptResource.Items[i].FindControl("lbl_resourcetype");
                Label lbl_resourceid = (Label)rptResource.Items[i].FindControl("lbl_resourceid");
               // Label lbl_paperid = (Label)rptResource.Items[i].FindControl("lbl_paperid");

                if (lbl_resourcetype.Text == Convert.ToInt16(CoursersClass.Courseware).ToString())
                {
                    VOTResource voResource = opCourse.LoadResource(int.Parse(lbl_resourceid.Text));
                    if (voResource.ResourseState != 1)
                    {
                        voResource.ResourseState = 1;
                        opCourse.UpdateResource(voResource);
                    }
                }
                else if (lbl_resourcetype.Text == Convert.ToInt16(CoursersClass.Paper).ToString())
                {
                    //VOPaper voPaper = opPaper.LoadPaper(item.CoursersResourceID);
                }
            }
        }
        chkAll.Checked = false;
        chkCancel.Checked = false;
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_AuditingStateNo_Click(object sender, EventArgs e)
    {
        //冻结
        for (int i = 0; i < rptResource.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptResource.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptResource.Items[i].FindControl("lblid");
                Label lbl_resourcetype = (Label)rptResource.Items[i].FindControl("lbl_resourcetype");
                Label lbl_resourceid = (Label)rptResource.Items[i].FindControl("lbl_resourceid");
              //  Label lbl_paperid = (Label)rptResource.Items[i].FindControl("lbl_paperid");

                if (lbl_resourcetype.Text == Convert.ToInt16(CoursersClass.Courseware).ToString())
                {
                    VOTResource voResource = opCourse.LoadResource(int.Parse(lbl_resourceid.Text));
                    if (voResource.ResourseState != 0)
                    {
                        voResource.ResourseState = 0;
                        opCourse.UpdateResource(voResource);
                    }
                }
                else if (lbl_resourcetype.Text == Convert.ToInt16(CoursersClass.Paper).ToString())
                {
                    //VOPaper voPaper = opPaper.LoadPaper(item.CoursersResourceID);
                }
            }
        }
        chkAll.Checked = false;
        chkCancel.Checked = false;
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";//0简单搜索1高级搜索
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_del_Click(object sender, EventArgs e)
    {
        //删除资源
        for (int i = 0; i < rptResource.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptResource.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptResource.Items[i].FindControl("lblid");
                opCourse.DelCourseResource(int.Parse(lblid.Text));
            }
        }
        chkAll.Checked = false;
        chkCancel.Checked = false;
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);

        InitPage();
    }
}